/**
 * @filename:FCommentController 2020-08-19
 * @project shop-cloud  0.0.1
 * Copyright(c) 2020 yb Co. Ltd. 
 * All right reserved. 
 */
package com.example.demo.utils;

import cn.hutool.core.io.file.FileReader;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.io.File;

/**
 * <p>自动生成工具：mybatis-dsc-generator</p>
 * 
 * <p>说明： 帖子主评论表API接口层</P>
 * @version: 0.0.1
 * @author: yb
 *
 */
@Api(tags = "",value="" )
@RestController
@RequestMapping("/f/test")
@Slf4j
public class TestController {
    @ResponseBody
    @GetMapping("/file")
    @ApiOperation(value = "", notes = "评论列表")
    public String file(@RequestParam String filePath){
        JSONObject json = new JSONObject(); //json文件
        json.put("name", "module");
        json.put("file", "module");
        json.put("type", "dir");
        json.put("_level", 0);
        json.put("repeat", "ignore");
        json.put("children", JSONUtil.createArray());
        folderMethod2(filePath, json);
        return json.toString();
    }



    public static void folderMethod2(String path, JSONObject json) {
        File file = new File(path);
        if (file.exists()) {
            File[] files = file.listFiles();
            if (null != files) {
                for (File file2 : files) {
                    if (!file2.getName().equals("target") & !file2.getName().endsWith(".idea") & !file2.getName().endsWith(".git") & !file2.getName().endsWith(".gitignore")  & !file2.getName().equals(".demo2") &!file2.getName().endsWith(".mvn") &!file2.getName().endsWith(".iml") &!file2.getName().endsWith("HELP.md") &!file2.getName().endsWith("mvnw") &!file2.getName().endsWith("mvnw.cmd") ){
                        if (file2.isDirectory()) {
                            System.out.println("文件夹:" + file2.getAbsolutePath());
                            JSONObject jsonDir = new JSONObject(); //json文件
                            jsonDir.put("name", file2.getName());
                            jsonDir.put("file", file2.getName());
                            jsonDir.put("type", "dir");
                            jsonDir.put("_level", (Integer)json.get("_level") + 1);
                            jsonDir.put("repeat", "ignore");
                            jsonDir.put("children", JSONUtil.createArray());
                            JSONArray children = (JSONArray)json.get("children");

                            children.add(jsonDir);
                            json.put("children", children);
                            folderMethod2(file2.getAbsolutePath(), jsonDir);
                        } else {
                            System.out.println("文件:" + file2.getAbsolutePath());
                            JSONObject jsonFile = new JSONObject(); //json文件
                            jsonFile.put("name", file2.getName());
                            jsonFile.put("file", file2.getName());
                            jsonFile.put("type", "file");
                            jsonFile.put("_level", (Integer)json.get("_level") + 1);
                            jsonFile.put("repeat", "replace");
                            jsonFile.put("expanded", true);

                            jsonFile.put("template", new FileReader(file2.getAbsolutePath()).readString());
                            JSONArray children = (JSONArray)json.get("children");
                            children.add(jsonFile);
                            json.put("children", children);
                        }
                    }
                }
            }
        } else {
            System.out.println("文件不存在!");
        }
    }


}